from django.shortcuts import render, redirect, HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from App001 import models
import json
from django.forms.utils import ErrorDict
from App001.utils.bootstrap import BootStrapModelForm


class TaskModelForm(BootStrapModelForm):
    class Meta:
        model = models.Task
        fields = "__all__"


def task_list(request):
    quaryset = models.Task.objects.all().order_by
    form = TaskModelForm()
    contest = {
        "form": form,
        "quaryset": quaryset
    }
    return render(request, "task_list.html", contest)


@csrf_exempt
def task_ajax(request):
    # print(request.GET)
    # print(request.POST)
    data_dict = {"status": True, 'data': [11, 22, 33, 44]}
    return HttpResponse(json.dumps(data_dict))


@csrf_exempt
def task_add(request):
    form = TaskModelForm(data=request.POST)
    if form.is_valid():

        form.save()
        data_dict = {"status": True}
        return HttpResponse(json.dumps(data_dict))

    # print()
    data_dict = {"status": False, "error": form.errors}
    return HttpResponse(json.dumps(data_dict, ensure_ascii=False))
